package com.snj.visangedu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.directionalviewpager.DirectionalViewPager;
import com.snj.visangedu.lib.NewsLib;


public class ContentActivity extends FragmentActivity {
	
	String tag = "ContentActivity";
	int viewpagercurrentpos = 1;
	int currentposition = 0;
	
	int newsid = 1;
	
	String no = "";
	String prev = "", next = "";
	
	String type = "1";
	
	ImageView img_next = null;
	ImageView img_prev = null;
	ImageView img_close = null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Intent contentintent = getIntent();
		
		type = contentintent.getStringExtra("type");
		if(type == null) type = "1";
		
		newsid = contentintent.getIntExtra("newsid", 1);
		
		no = contentintent.getStringExtra("no");
		prev = contentintent.getStringExtra("prev");
		next = contentintent.getStringExtra("next");
		
		currentposition = contentintent.getIntExtra("position", 0);
		
		setContentView(R.layout.activity_content);
		
		img_next = (ImageView)findViewById(R.id.img_nextnews);
		img_prev = (ImageView)findViewById(R.id.img_prevnews);
		img_close = (ImageView)findViewById(R.id.img_closenews);
		
		
		
		final TestFragmentAdapter adapter = new TestFragmentAdapter(getSupportFragmentManager(), newsid);
		adapter.SetDataset(prev, no, next, currentposition - 1, currentposition, currentposition + 1);
		
		 final DirectionalViewPager pager = (DirectionalViewPager)findViewById(R.id.pager);
	        pager.setAdapter(adapter);
	        pager.setOrientation(DirectionalViewPager.VERTICAL);
	        
	        pager.setCurrentItem(viewpagercurrentpos);
	        
	        	        
	        
			img_next.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					if(currentposition >= (NewsLib.newsloader[newsid].GetNewsArray().size() - 1))
						return;
					
					Intent contentintent = new Intent(getApplicationContext(), ContentActivity.class);
					contentintent.putExtra("newsid", newsid);
					
					currentposition++;
					
					
					if(currentposition > 0)
						contentintent.putExtra("prev", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition-1).GetNo());
					
					
					contentintent.putExtra("no", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition).GetNo());
					contentintent.putExtra("position", currentposition);
					if(currentposition < (NewsLib.newsloader[newsid].GetNewsArray().size() - 1))
						contentintent.putExtra("next", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition+1).GetNo());
					startActivity(contentintent);
					ContentActivity.this.overridePendingTransition(0, 0);
					
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finish();
				}
			});
			
			img_prev.setOnClickListener(new OnClickListener() {
						
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(currentposition > 0)
					{
						Intent contentintent = new Intent(getApplicationContext(), ContentActivity.class);
						contentintent.putExtra("newsid", newsid);
						
						currentposition--;
						
						if(currentposition > 0)
							contentintent.putExtra("prev", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition-1).GetNo());
						
						contentintent.putExtra("no", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition).GetNo());
						contentintent.putExtra("position", currentposition);
						if(currentposition < (NewsLib.newsloader[newsid].GetNewsArray().size() - 1))
							contentintent.putExtra("next", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition+1).GetNo());
						startActivity(contentintent);
						ContentActivity.this.overridePendingTransition(0, 0);
						
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						finish();
					}
				}
			});
			
			img_close.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			});
	        
	        pager.setOnPageChangeListener(new OnPageChangeListener() {
				
				@Override
				public void onPageSelected(int position) {
					// TODO Auto-generated method stub
					
					Log.d(tag, "PageChange" + newsid);
					
					ContentFragment frg = (ContentFragment)adapter.getItem(position);
					//Log.d(tag, frg.mContent);
					
					if(position == 0 && currentposition > 0)
					{
						Intent contentintent = new Intent(getApplicationContext(), ContentActivity.class);
						contentintent.putExtra("newsid", newsid);
						
						currentposition--;
						
						if(currentposition > 0)
							contentintent.putExtra("prev", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition-1).GetNo());
						
						contentintent.putExtra("no", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition).GetNo());
						contentintent.putExtra("position", currentposition);
						if(currentposition < (NewsLib.newsloader[newsid].GetNewsArray().size() - 1))
							contentintent.putExtra("next", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition+1).GetNo());
						startActivity(contentintent);
						ContentActivity.this.overridePendingTransition(0, 0);
						
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						finish();
					}
					else if(position == 2)
					{						
						
						Intent contentintent = new Intent(getApplicationContext(), ContentActivity.class);
						contentintent.putExtra("newsid", newsid);
						
						currentposition++;
						
						if(currentposition > 0)
							contentintent.putExtra("prev", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition-1).GetNo());
						
						contentintent.putExtra("no", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition).GetNo());
						contentintent.putExtra("position", currentposition);
						if(currentposition < (NewsLib.newsloader[newsid].GetNewsArray().size() - 1))
							contentintent.putExtra("next", NewsLib.newsloader[newsid].GetNewsArray().get(currentposition+1).GetNo());
						startActivity(contentintent);
						ContentActivity.this.overridePendingTransition(0, 0);
						
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						finish();
					}
					
					viewpagercurrentpos = position;
					
				}
				
				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onPageScrollStateChanged(int arg0) {
					// TODO Auto-generated method stub
					
					
					
				}
			});
	}

}
